<template>
  <page title="需求表详情" has-back>
    <basic-info-form v-model="form.project"></basic-info-form>
    <el-row justify="center" v-if="approveShow">
      <el-col :span="15">
        <el-row justify="end" :gutter="10">
          <el-button type="primary" plain @click="$router.push('classroomResources')">查看教室资源状态</el-button>
        </el-row>
      </el-col>
    </el-row>
    <classroom-needs-form v-model="form.classroomNeed" :editable="edit" :dates="form.dates"></classroom-needs-form>
    <approve-form v-model="approve" :edit="approveShow"></approve-form>
    <el-row justify="center">
      <el-button type="primary" @click="edit=false" v-if="edit">保存</el-button>
      <el-button type="primary" @click="submit()" v-else-if="approveShow">确认提交</el-button>
      <el-button @click="$router.back()">返回</el-button>
    </el-row>
  </page>
</template>

<script>
import Page from "@/components/Page";
import ClassroomNeedsForm from "@/components/ClassroomNeedsForm";
import BasicInfoForm from "@/components/BasicInfoForm";
import ApproveForm from "@/components/ApproveForm";
import axios from "axios";
export default {
  name: "ClassroomNeedApprove",
  components:{ClassroomNeedsForm, Page,BasicInfoForm,ApproveForm},
  data(){
    return{
      form: {
        project:{
          name: '',
          startDate: '',
          endDate: '',
          people: '',
          part: {},
          user: {},
        },
        classroomNeed:{
          detailList:[],
          detail:"",
        },
        dates:[]
      },
      approve:{
        result:true,
        detail:''
      },
      edit:false
    }
  },
  methods:{
    submit(){
      let msg;
      if(this.approve.result){
        msg = '通过'
      }else{
        msg = '驳回'
      }
      this.$confirm('确认要'+msg+'这份申请，'+msg+'后不可修改，继续？', '注意', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.updateApprove(this.approve);
      });
    },
    findFormById(id){
      return axios.get(this.$host+"/form/"+id+"?type=class").then(response=>{
        if(response.data.code === 0){
          this.form = response.data.data;
          console.log(this.form);
          console.log(this.approveShow);
        }else{
          this.$message.error(response.data.msg);
        }
      })
    },
    findApproveByUidAndType(uid,type){
      return axios.get(this.$host+"/approve",{
        params:{
          uid:uid,
          type:type
        }
      }).then(response=>{
        if(response.data.code === 0){
          this.approve = response.data.data;
        }
      })
    },
    updateApprove(data){
      return axios.put(this.$host+"/approve",data).then(response=>{
        if(response.data.code === 0){
          this.$message({
            type: 'success',
            message: '提交成功!'
          });
          this.$router.replace("classroomRA");
        }else{
          this.$message.error(response.data.msg);
        }
      })
    }
  },
  created() {
    let param = this.$router.currentRoute.value.query;
    this.findFormById(param.id).then(()=>this.findApproveByUidAndType(this.form.project.uid,2));
  },
  computed:{
    approveShow(){
      return this.form.classroomNeed.needStatus === 0;
    }
  }
}
</script>

<style scoped>

</style>